package com.zjht.gmcc.servlet.provider;

import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.zjht.gmcc.bean.BaseServlet;

import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyPath;

public class AddProvider extends BaseServlet {

	private static final long serialVersionUID = 2205937410942000119L;

	public boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC, String menu)
			throws Exception {
		int i_flag = 0;
		PreparedStatement ps, ps1;
		ResultSet rs;

		// ---上传图片用的参数
		// String saveDirectory =request.getRealPath("/").toString()+"//phonepic/";
		String privodeid = "";
	
		String provideName = request.getParameter("pName");// 供应商名称
		String provideType = request.getParameter("qyxz");// 供应商类型
		String legalPerson = request.getParameter("pfrdb");// 法人pfrdb
		String firmage =  request.getParameter("pDate");// 成立时间
		String regcapital = request.getParameter("zczb");// 注册资本
		String sslb = request.getParameter("sslb");// 所属类别
		String rangetxt = request.getParameter("rangetxt");// 经营范围
		String reglicence = request.getParameter("reglicence");// 营业执照
		String IDpath = request.getParameter("IDpath");// 联系人身份证
		String legalPersonID = request.getParameter("legalPersonID");// 法人身份证
		String orgCode = request.getParameter("orgCode");// 组织编码
		String taxPath = request.getParameter("taxpath");// 纳税
		String proCode=request.getParameter("pCode"); 
		//
		String address =request.getParameter("address"); 
		String posCode = request.getParameter("posCode"); 
		String website = request.getParameter("website");
		String linkName = request.getParameter("linkName");
		String IDnum =  request.getParameter("IDnum");
		String s_pictype = null; 
		String fax = request.getParameter("fax");
		String phone =  request.getParameter("phone");
		String email =  request.getParameter("email");
		String firstName = "";
		int server =Integer.parseInt( request.getParameter("serverid"));
		String ser = "";
		String serList[] = null;
		
		try {

			String s_sql = "SELECT SEQ_PROVIDER.nextval FROM dual";
			ps = myJDBC.prepareStatement(s_sql);

			rs = ps.executeQuery();
			if (rs.next()) {
				privodeid = rs.getString(1);
				i_flag = 1;
			}

			
			
			
			if (i_flag == 1) {//拆解服务能力表
				s_sql = "INSERT INTO PROVIDERDETAIL (PROVIDERID,BRAND,PRODUSTTYPE,SALEZONE,VALIDDATE,FILEPATH,QUANTITY) " + "VALUES (?,?,?,?,?,?,?)";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, privodeid);
				for (int i = 0; i < server+1; i++) {
					if(request.getParameter("ppara_add["+i +"]brand")!=null){
						ps.setString(2, request.getParameter("ppara_add["+i +"]brand"));
						ps.setString(3, request.getParameter("ppara_add["+i +"]protype"));
						ps.setString(4, request.getParameter("ppara_add["+i +"]area"));
						ps.setString(5, request.getParameter("ppara_add["+i +"]vDate"));
						ps.setString(6, request.getParameter("ppara_add["+i +"]file"));
						ps.setString(7, request.getParameter("ppara_add["+i +"]pjxs"));
						i_flag = ps.executeUpdate();
					}
					
				}
				
			} 
		
			if (i_flag == 1) {
				s_sql = "INSERT INTO PROVIDER (PROVIDERID,PROVIDERCODE,PROVIDERNAME,LEGALPERSON,PROVIDERTYPE,FIRMAGE,REGCAPITAL,TRADEID,BUSINESS,ADDRESS,HOMEPAGE,LINKMAN,IDCARD,"
						+ "FAX,LINKPHONE,EMAIL,LEGALPERSONID,ORGCODE,IDFILEPATH,REGLICENCE,REGTAX,PERMITSTATUS,ORDERSTATUS,RANGETYPE,STATUS,PROORGCODE) "
						+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0,0,0,1,0)";
				ps = myJDBC.prepareStatement(s_sql);
				ps.setString(1, privodeid);
				ps.setString(2, proCode);
				ps.setString(3, provideName);
				ps.setString(4, legalPerson);
				ps.setString(5, provideType);
				ps.setString(6, firmage);
				ps.setString(7, regcapital);
				ps.setString(8, sslb); 
				ps.setString(9, rangetxt);
				ps.setString(10, address);
				ps.setString(11, website);
				ps.setString(12, linkName);
				ps.setString(13, IDnum);
				ps.setString(14, fax);
				ps.setString(15, phone);
				ps.setString(16, email);
				ps.setString(17, legalPersonID);
				ps.setString(18, orgCode);
				ps.setString(19, IDpath);
				ps.setString(20, reglicence);
				ps.setString(21, taxPath);
				i_flag = ps.executeUpdate();
			}

			if (i_flag == 1) {
				alertAndReturnValue(out, "新增供应商成功!", "1");
			} else {
				errAlert(request, out, menu, "提交错误");
			}
			return i_flag == 1;

		} catch (Exception e) {
			throw e;
		} finally {

			
		}

	}

}
